﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.FileSystem;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;

    public class ThumbFunctions
    {
        public int _ImageQuality = 0x55;

        public void CreateAllThumbsOnDisk(int PortalID, string Lang, string ThumbSize)
        {
            ArrayList filesByFolder;
            IEnumerator enumerator2;
            ProductController controller = new ProductController();
            PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
            FolderInfo folder = FileSystemUtils.GetFolder(PortalID, "productthumbs");
            if (folder != null)
            {
                IEnumerator enumerator;
                filesByFolder = FileSystemUtils.GetFilesByFolder(PortalID, folder.FolderID);
                try
                {
                    enumerator = filesByFolder.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        DotNetNuke.Services.FileSystem.FileInfo current = (DotNetNuke.Services.FileSystem.FileInfo) enumerator.Current;
                        try
                        {
                            FileSystemUtils.DeleteFile(currentPortalSettings.HomeDirectoryMapPath + @"productthumbs\" + current.FileName, currentPortalSettings, true);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            ProjectData.ClearProjectError();
                        }
                        current = null;
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            folder = null;
            filesByFolder = controller.GetProductExportList(PortalID, Lang, false);
            try
            {
                enumerator2 = filesByFolder.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    NB_Store_ProductsInfo info3 = (NB_Store_ProductsInfo) enumerator2.Current;
                    this.CreateProductThumbsOnDisk(info3.ProductID, Lang, ThumbSize);
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
        }

        public void CreateProductThumbsOnDisk(int PortalID, int ProductID)
        {
            string storeSetting = SharedFunctions.GetStoreSetting(PortalID, "diskthumbnails.size");
            this.CreateProductThumbsOnDisk(ProductID, SharedFunctions.GetCurrentCulture(), storeSetting);
        }

        public void CreateProductThumbsOnDisk(int ProductID, string Lang, string ThumbSize)
        {
            IEnumerator enumerator;
            ArrayList productImageList = new ProductController().GetProductImageList(ProductID, Lang);
            try
            {
                enumerator = productImageList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_ProductImageInfo current = (NB_Store_ProductImageInfo) enumerator.Current;
                    this.CreateThumbOnDisk(current.ImagePath, ThumbSize);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void CreateThumbOnDisk(string ImgPathName, string ThumbSize)
        {
            string thumbW = "";
            string thumbH = "";
            if ((ThumbSize != "") && (ImgPathName != ""))
            {
                string[] strArray = ThumbSize.Split(new char[] { ',' });
                int upperBound = strArray.GetUpperBound(0);
                for (int i = 0; i <= upperBound; i++)
                {
                    thumbW = this.getThumbWidth(strArray[i]);
                    thumbH = this.getThumbHeight(strArray[i]);
                    string filename = this.GetThumbFilePathName(ImgPathName, thumbW, thumbH);
                    using (Bitmap bitmap = this.GetThumbnailExtra(ImgPathName, Conversions.ToInteger(thumbW), Conversions.ToInteger(thumbH)))
                    {
                        if (bitmap != null)
                        {
                            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                            EncoderParameters encoderParams = new EncoderParameters(1);
                            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long) this._ImageQuality);
                            try
                            {
                                bitmap.Save(filename, imageEncoders[1], encoderParams);
                            }
                            catch (Exception exception1)
                            {
                                ProjectData.SetProjectError(exception1);
                                Exception exception = exception1;
                                GC.Collect();
                                try
                                {
                                    bitmap.Save(filename, imageEncoders[1], encoderParams);
                                }
                                catch (Exception exception3)
                                {
                                    ProjectData.SetProjectError(exception3);
                                    Exception exception2 = exception3;
                                    ProjectData.ClearProjectError();
                                }
                                ProjectData.ClearProjectError();
                            }
                            bitmap.Dispose();
                        }
                    }
                }
            }
        }

        public string GetThumbFileName(string ImgPathName, string ThumbW, string ThumbH)
        {
            return (Path.GetFileNameWithoutExtension(ImgPathName) + "_" + ThumbW + "x" + ThumbH + Path.GetExtension(ImgPathName));
        }

        public string GetThumbFilePathName(string ImgPathName, string ThumbW, string ThumbH)
        {
            return Strings.Replace(ImgPathName, @"productimages\" + Path.GetFileName(ImgPathName), @"productthumbs\" + this.GetThumbFileName(ImgPathName, ThumbW, ThumbH), 1, -1, CompareMethod.Binary);
        }

        public string getThumbHeight(string ThumbSize)
        {
            string str2 = "0";
            if (!Versioned.IsNumeric(ThumbSize) & (ThumbSize != ""))
            {
                string[] strArray = ThumbSize.Split(new char[] { 'x' });
                if (strArray.GetUpperBound(0) == 1)
                {
                    str2 = strArray[1];
                }
            }
            return str2;
        }

        public Bitmap GetThumbnailExtra(string strFilepath, int intMaxWidth, int intMaxHeight)
        {
            Bitmap image = null;
            ImageFormat jpeg = null;
            Bitmap bitmap3 = null;
            string str = "";
            if (intMaxHeight != 0)
            {
                if (intMaxHeight > intMaxWidth)
                {
                    str = "P";
                }
                else if (intMaxHeight == intMaxWidth)
                {
                    str = "S";
                }
                else
                {
                    str = "L";
                }
            }
            else
            {
                str = "";
                intMaxHeight = intMaxWidth;
            }
            if (File.Exists(strFilepath))
            {
                using (Bitmap bitmap4 = new Bitmap(strFilepath))
                {
                    jpeg = ImageFormat.Jpeg;
                    int width = bitmap4.Width;
                    int height = bitmap4.Height;
                    if (((intMaxWidth > 0) & (intMaxWidth < width)) | ((intMaxHeight > 0) & (intMaxHeight < height)))
                    {
                        int num4;
                        int num5;
                        double num3 = bitmap4.PhysicalDimension.Width / bitmap4.PhysicalDimension.Height;
                        if (bitmap4.PhysicalDimension.Height < bitmap4.PhysicalDimension.Width)
                        {
                            num5 = intMaxWidth;
                            num4 = (int) Math.Round((double) (((double) intMaxWidth) / num3));
                        }
                        else
                        {
                            num5 = (int) Math.Round((double) (intMaxWidth * num3));
                            num4 = intMaxWidth;
                        }
                        switch (str)
                        {
                            case "":
                            {
                                bitmap3 = new Bitmap(num5, num4);
                                Graphics graphics = Graphics.FromImage(bitmap3);
                                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics.FillRectangle(Brushes.White, 0, 0, num5, num4);
                                graphics.DrawImage(bitmap4, 0, 0, num5, num4);
                                goto Label_0427;
                            }
                            case "L":
                            {
                                num5 = intMaxWidth;
                                num4 = (int) Math.Round((double) (((double) intMaxWidth) / num3));
                                image = new Bitmap(num5, num4);
                                Graphics graphics3 = Graphics.FromImage(image);
                                graphics3.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics3.FillRectangle(Brushes.White, 0, 0, num5, num4);
                                graphics3.DrawImage(bitmap4, 0, 0, num5, num4);
                                Rectangle destRect = new Rectangle(0, 0, num5, intMaxHeight);
                                bitmap3 = new Bitmap(num5, intMaxHeight);
                                Graphics graphics2 = Graphics.FromImage(bitmap3);
                                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics2.FillRectangle(Brushes.White, 0, 0, num5, intMaxHeight);
                                graphics2.DrawImage(image, destRect, 0, (int) Math.Round((double) (((double) (image.Height - intMaxHeight)) / 2.0)), num5, intMaxHeight, GraphicsUnit.Pixel);
                                goto Label_0427;
                            }
                            case "P":
                            {
                                num5 = (int) Math.Round((double) (intMaxHeight * num3));
                                num4 = intMaxHeight;
                                image = new Bitmap(num5, num4);
                                Graphics graphics5 = Graphics.FromImage(image);
                                graphics5.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics5.FillRectangle(Brushes.White, 0, 0, num5, num4);
                                graphics5.DrawImage(bitmap4, 0, 0, num5, num4);
                                Rectangle rectangle2 = new Rectangle(0, 0, intMaxWidth, num4);
                                bitmap3 = new Bitmap(intMaxWidth, num4);
                                Graphics graphics4 = Graphics.FromImage(bitmap3);
                                graphics4.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics4.FillRectangle(Brushes.White, 0, 0, intMaxWidth, num4);
                                graphics4.DrawImage(image, rectangle2, (int) Math.Round((double) (((double) (image.Width - intMaxWidth)) / 2.0)), 0, intMaxWidth, num4, GraphicsUnit.Pixel);
                                goto Label_0427;
                            }
                        }
                        if (str == "S")
                        {
                            if (bitmap4.PhysicalDimension.Height < bitmap4.PhysicalDimension.Width)
                            {
                                num5 = (int) Math.Round((double) (intMaxHeight * num3));
                                num4 = intMaxHeight;
                            }
                            else
                            {
                                num5 = intMaxWidth;
                                num4 = (int) Math.Round((double) (((double) intMaxWidth) / num3));
                            }
                            image = new Bitmap(num5, num4);
                            Graphics graphics7 = Graphics.FromImage(image);
                            graphics7.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics7.FillRectangle(Brushes.White, 0, 0, num5, num4);
                            graphics7.DrawImage(bitmap4, 0, 0, num5, num4);
                            Rectangle rectangle3 = new Rectangle(0, 0, intMaxWidth, intMaxHeight);
                            bitmap3 = new Bitmap(intMaxWidth, intMaxHeight);
                            Graphics graphics6 = Graphics.FromImage(bitmap3);
                            graphics6.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics6.FillRectangle(Brushes.White, 0, 0, intMaxWidth, intMaxHeight);
                            graphics6.DrawImage(image, rectangle3, 0, (int) Math.Round((double) (((double) (image.Height - intMaxHeight)) / 2.0)), intMaxWidth, intMaxHeight, GraphicsUnit.Pixel);
                        }
                    }
                    else
                    {
                        bitmap3 = bitmap4;
                        bitmap3.SetResolution(72f, 72f);
                    }
                Label_0427:
                    if (bitmap4 != null)
                    {
                        bitmap4.Dispose();
                    }
                }
            }
            return bitmap3;
        }

        public string GetThumbURLName(string ImgPathName, string ThumbW, string ThumbH)
        {
            return Strings.Replace(ImgPathName, "productimages/" + Path.GetFileName(ImgPathName), "productthumbs/" + this.GetThumbFileName(ImgPathName, ThumbW, ThumbH), 1, -1, CompareMethod.Binary);
        }

        public string getThumbWidth(string ThumbSize)
        {
            if (!Versioned.IsNumeric(ThumbSize) & (ThumbSize != ""))
            {
                return ThumbSize.Split(new char[] { 'x' })[0];
            }
            return ThumbSize;
        }
    }
}

